package cn.t11.dm.user.service.impl;

import cn.t11.dm.user.entity.DmUser;
import cn.t11.dm.user.exception.UserExceptionEnum;
import cn.t11.dm.user.service.IDmUserService;
import cn.t11.dm.user.service.LocalDmUserService;
import cn.t11.dm.user.util.Constants;

import cn.t11.dm.user.vo.TokenVO;
import cn.t11.dm.user.vo.UserVO;
import cn.t11.pojo.Dto;
import cn.t11.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class LocalDmUserServiceImpl implements LocalDmUserService{

    @Autowired
    private IDmUserService iDmUserService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Dto<List> doLogin(String phone, String password) throws Exception {

        // 如果已经登录，就直接返回token
        String token = (String) redisUtil.get(Constants.LONINED_USER_PREFIX + phone);
        if (null != token) {
            TokenVO tokenVO = new TokenVO();
            tokenVO.setToken(token);
            return DtoUtil.returnDataSuccess(tokenVO);
       }
        // 没有登录的时候
        DmUser dmUser = new DmUser();
        dmUser.setPhone(phone);
        dmUser.setPassword(password);
        DmUser user = iDmUserService.getDmUserBy(dmUser);
        System.out.println(user.getPhone());

        // 登录成功的时候，把信息保存到redis
        if (null != user) {
            // 生成Token
            token = TokenUtil.generateToken(Constants.TOKEN_PREFIX, user.getPhone());
            TokenVO tokenVO = new TokenVO();
            tokenVO.setToken(token);
            // 保存到redis
            redisUtil.set(token, user, Constants.SESSION_TIMEOUT);
            redisUtil.set(Constants.LONINED_USER_PREFIX + phone, token);

            UserVO userVO = new UserVO();
            userVO.setUserId(user.getId());
            userVO.setPhone(phone);
            userVO.setWxUserId(user.getWxUserId());
            userVO.setRealName(user.getRealName());
            userVO.setNickName(user.getNickName());
            userVO.setSex(user.getSex());
            userVO.setIdCard(user.getIdCard());

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            userVO.setBirthday(simpleDateFormat.format(user.getBirthday()));
            userVO.setCreatedTime(simpleDateFormat.format(user.getCreatedTime()));
            userVO.setUpdatedTime(simpleDateFormat.format(user.getUpdatedTime()));
            userVO.setHobby(user.getHobby());
            userVO.setImageId(0);
            userVO.setImgUrl(null);

            // 返回List
            List<Object> list = new ArrayList<>();
            list.add(userVO);
            list.add(tokenVO);
            return DtoUtil.returnDataSuccess(list);
        }
        // 返回登录失败信息
        return DtoUtil.returnFail(UserExceptionEnum.UESR_AUTH_FAIL.getErrorMsg()
                , UserExceptionEnum.UESR_AUTH_FAIL.getErrorCode());
    }
}
